using System.Collections.Generic;
using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using Game.XH;
using UnityEngine;

namespace Game.Component
{
	public class BandageItem : BaseItem
	{
		private enum State
		{
			Normal = 0,
			Head = 1,
			LeftHand = 2,
			RightHand = 3,
			LeftLeg = 4,
			RightLeg = 5
		}

		private State state;

		private List<ItemComponentTrigger> triggers = new List<ItemComponentTrigger>();

		private ItemComponentTrigger target;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
		}

		protected override void OnItemCompTriggerEnter(ColliderData collData)
		{
			base.OnItemCompTriggerEnter(collData);
			if (collData.trigger != null && collData.item != null && collData.item.itemData.scriptType == 1 && ((CharacterItem)collData.item).CanUseBandage(collData.trigger) && (collData.trigger.type == ItemComponentTrigger.Type.Head || collData.trigger.type == ItemComponentTrigger.Type.LeftHand || collData.trigger.type == ItemComponentTrigger.Type.RightHand || collData.trigger.type == ItemComponentTrigger.Type.LeftLeg || collData.trigger.type == ItemComponentTrigger.Type.RightLeg) && !triggers.Contains(collData.trigger))
			{
				triggers.Add(collData.trigger);
				target = collData.trigger;
				CheckState();
			}
		}

		protected override void OnItemCompTriggerExit(ColliderData collData)
		{
			base.OnItemCompTriggerExit(collData);
			triggers.Remove(collData.trigger);
			if (collData.trigger == target)
			{
				if (triggers.Count > 0)
				{
					target = triggers[triggers.Count - 1];
				}
				else
				{
					target = null;
				}
				CheckState();
			}
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			base.TouchDown(touchEventData);
			triggers.Clear();
			target = null;
		}

		public override void OnAttachItemComplete(bool updatePosition)
		{
			base.OnAttachItemComplete(updatePosition);
			CheckState();
		}

		protected override bool ScriptCheckCollsStart()
		{
			bool flag = false;
			if (target != null)
			{
				if (target.type == ItemComponentTrigger.Type.LeftHand)
				{
					if (target.OnAttachItem(this, target) != null)
					{
						SetLocalPosition(base.itemData.putHandX, base.itemData.putHandY);
						flag = true;
					}
				}
				else if (target.type == ItemComponentTrigger.Type.RightHand)
				{
					if (target.OnAttachItem(this, target) != null)
					{
						SetLocalPosition(base.itemData.putHandX, base.itemData.putHandY);
						flag = true;
					}
				}
				else if (target.type == ItemComponentTrigger.Type.LeftLeg)
				{
					if (target.OnAttachItem(this, target) != null)
					{
						SetLocalPosition(base.itemData.putLegX, base.itemData.putLegY);
						flag = true;
					}
				}
				else if (target.type == ItemComponentTrigger.Type.RightLeg)
				{
					if (target.OnAttachItem(this, target) != null)
					{
						SetLocalPosition(base.itemData.putLegX, base.itemData.putLegY);
						flag = true;
					}
				}
				else if (target.type == ItemComponentTrigger.Type.Head && target.OnAttachItem(this, target) != null)
				{
					SetLocalPosition(base.itemData.putHeadX, base.itemData.putHeadY);
					flag = true;
				}
			}
			if (flag)
			{
				Singleton<AudioManager>.Instance.PlaySound("sound_explode");
				Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_explode_1", base.transform.position + new Vector3(0f, 0.4f, -0.0004f), 2f);
				Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_blink_1", base.transform.position + new Vector3(0f, 0.4f, -0.0004f), 3f);
			}
			return flag;
		}

		public override void TouchUp(TouchEventData touchEventData)
		{
			base.TouchUp(touchEventData);
			triggers.Clear();
			target = null;
			CheckState();
		}

		private void CheckState()
		{
			if (parentItem != null && parentItem.itemData.scriptType == 1)
			{
				if (attachType == AttachType.PutLeftHand)
				{
					SetState(State.LeftHand);
				}
				else if (attachType == AttachType.PutRightHand)
				{
					SetState(State.RightHand);
				}
				else if (attachType == AttachType.PutLeftLeg)
				{
					SetState(State.LeftLeg);
				}
				else if (attachType == AttachType.PutRightLeg)
				{
					SetState(State.RightLeg);
				}
				else if (attachType == AttachType.PutFace)
				{
					SetState(State.Head);
				}
				else if (attachType == AttachType.PutHead)
				{
					SetState(State.Head);
				}
			}
			else if (target != null && ((CharacterItem)target.owner).CanUseBandage(target))
			{
				if (target.type == ItemComponentTrigger.Type.LeftHand)
				{
					SetState(State.LeftHand);
				}
				else if (target.type == ItemComponentTrigger.Type.RightHand)
				{
					SetState(State.RightHand);
				}
				else if (target.type == ItemComponentTrigger.Type.Head || target.type == ItemComponentTrigger.Type.Face)
				{
					SetState(State.Head);
				}
				else if (target.type == ItemComponentTrigger.Type.LeftLeg)
				{
					SetState(State.LeftLeg);
				}
				else if (target.type == ItemComponentTrigger.Type.RightLeg)
				{
					SetState(State.RightLeg);
				}
				else
				{
					SetState(State.Normal);
				}
			}
			else
			{
				SetState(State.Normal);
			}
		}

		private void SetState(State val)
		{
			if (state == val)
			{
				return;
			}
			state = val;
			Transform transform;
			if (state == State.Head)
			{
				transform = model.Find("Head");
				model.SetLocalScaleX(1f);
			}
			else if (state == State.LeftHand)
			{
				transform = model.Find("Hand");
				model.SetLocalScaleX(1f);
			}
			else if (state == State.RightHand)
			{
				transform = model.Find("Hand");
				model.SetLocalScaleX(1f);
			}
			else if (state == State.LeftLeg)
			{
				transform = model.Find("Leg");
				transform.SetLocalX(0.05f);
				model.SetLocalScaleX(-1f);
			}
			else if (state == State.RightLeg)
			{
				transform = model.Find("Leg");
				transform.SetLocalX(0.07f);
				model.SetLocalScaleX(1f);
			}
			else
			{
				transform = model.Find("Normal");
				model.SetLocalScaleX(1f);
			}
			int childCount = model.childCount;
			for (int i = 0; i < childCount; i++)
			{
				Transform child = model.GetChild(i);
				if (child == transform)
				{
					child.gameObject.SetActive(true);
				}
				else
				{
					child.gameObject.SetActive(false);
				}
			}
			coll.offset = new Vector2(0f, transform.localPosition.y);
			coll.size = transform.GetComponent<BoxCollider2D>().size;
		}

		public override void UpdateFrontDepth()
		{
			base.UpdateFrontDepth();
			if (parentItem != null && parentItem.itemData.scriptType == 1)
			{
				if (attachType == AttachType.PutLeftHand)
				{
					base.transform.SetZ(((CharacterItem)parentItem).leftHand.lowerArmClothing.transform.position.z);
				}
				else if (attachType == AttachType.PutRightHand)
				{
					base.transform.SetZ(((CharacterItem)parentItem).rightHand.lowerArmClothing.transform.position.z);
				}
				else if (attachType == AttachType.PutLeftLeg)
				{
					base.transform.SetZ(((CharacterItem)parentItem).leftLeg.lowerLegClothing.transform.position.z);
				}
				else if (attachType == AttachType.PutRightLeg)
				{
					base.transform.SetZ(((CharacterItem)parentItem).rightLeg.lowerLegClothing.transform.position.z);
				}
			}
		}

		public override bool CanPlaceInBody()
		{
			return false;
		}

		public override bool CanPlaceInFace()
		{
			return false;
		}

		public override bool CanPlaceInHand()
		{
			return false;
		}

		public override bool CanPlaceInHead()
		{
			return false;
		}
	}
}
